<template>


  <el-row :gutter="20">
    <el-col :span="4"><span>编辑信息</span><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="8"><div class="grid-content bg-purple">
      <div class="app-container">
        <el-form ref="form" :model="member" label-width="80px" s>
          <el-form-item label="姓名">
            <el-input v-model="member.name" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="会员等级">
            <el-select v-model="member.levelId">
              <el-option v-for="item in level" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="member.sex">
              <el-option label="男" :value="0"></el-option>
              <el-option label="女" :value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="手机">
            <el-input v-model="member.tel" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="member.nickName" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="生日">
            <el-col :span="11">
              <el-date-picker type="date" placeholder="选择生日" v-model="member.birthday" style="width: 100%;"></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="身份证">
            <el-input v-model="member.idCard" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="城市">
            <el-input v-model="member.city" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="职业">
            <el-input v-model="member.job" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="身高">
          <el-input v-model="member.height" style="width: 200px"></el-input>
        </el-form-item>
          <el-form-item label="体重">
            <el-input v-model="member.weight" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="健身经验">
            <el-select v-model="member.sex">
              <el-option label="健身小白" :value="0"></el-option>
              <el-option label="健身爱好者" :value="1"></el-option>
              <el-option label="健身教练" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="健身目的">
            <el-select v-model="member.sex">
              <el-option label="减脂" :value="0"></el-option>
              <el-option label="增脂" :value="1"></el-option>
              <el-option label="增肌" :value="2"></el-option>
              <el-option label="修身" :value="3"></el-option>
            </el-select>
          </el-form-item>

<!--          <el-form-item label="标签">-->
<!--            <el-tag-->
<!--              :key="tag"-->
<!--              v-for="tag in dynamicTags"-->
<!--              closable-->
<!--              :disable-transitions="false"-->
<!--              @close="handleClose(tag)">-->
<!--              {{tag}}-->
<!--            </el-tag>-->
<!--            <el-input-->
<!--              class="input-new-tag"-->
<!--              v-if="inputVisible"-->
<!--              v-model="inputValue"-->
<!--              ref="saveTagInput"-->
<!--              size="small"-->
<!--              @keyup.enter.native="handleInputConfirm"-->
<!--              @blur="handleInputConfirm"-->
<!--            >-->
<!--            </el-input>-->
<!--            <el-button v-else class="button-new-tag" size="small" @click="showInput">添加标签</el-button>-->
<!--          </el-form-item>-->
          <el-form-item label="健身教练">
            <el-select v-model="member.coachId">
                <el-option v-for="item in coach" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:8080/upload/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="img" :src="img" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>


          <el-form-item>
            <el-button :disabled="saveBtnDisabled" type="primary" @click="edit">保存</el-button>
          </el-form-item>
        </el-form>

      </div>
    </div></el-col>
  </el-row>
</template>
<script>
import member from "../../../api/member";
import coach from "../../../api/coach";
import level from "../../../api/level";
import card from "../../../api/card";
  export default {
    data(){
      return{
        member:{
          sex: ""
        },
        saveBtnDisabled:false,
        dynamicTags: [], //标签数组
        inputVisible: false,
        inputValue: '', //输入的标签
        dialogVisible: false,
        img:"",
        coach:[],      //教练
        level:[]
      }
    },
    created() {
      this.queryById(this.$route.params.id);
      coach.getList(1,100).then(res=>{
        this.coach=res.data.data;
      })
      level.getList().then(res=>{
        this.level=res.data;
      })
    },
    methods: {
      edit(){
          member.modify(this.member).then(res=>{
            this.$message.success(res.message)
            this.$router.push({path:`/account/member`})
          })
        },
      queryById(id){
        member.getById(id).then(res=>{
          this.member = res.data
          this.img='http://rj0387v46.hn-bkt.clouddn.com/'+this.member.img;
        })

      },
      showInput() {
        this.inputVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },

      handleInputConfirm() {
        let inputValue = this.inputValue;
        if (inputValue) {
          this.dynamicTags.push(inputValue);
        }
        this.inputVisible = false;
        this.inputValue = '';
      },

      handleAvatarSuccess(res, file) {
        this.img = "http://rj0387v46.hn-bkt.clouddn.com/"+res.data;
        this.member.img=res.data;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'||file.type === 'image/jpg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
